#!/usr/bin/env bash
#
#/**
# * Copyright 2007 The Apache Software Foundation
# *
# * Licensed to the Apache Software Foundation (ASF) under one
# * or more contributor license agreements.  See the NOTICE file
# * distributed with this work for additional information
# * regarding copyright ownership.  The ASF licenses this file
# * to you under the Apache License, Version 2.0 (the
# * "License"); you may not use this file except in compliance
# * with the License.  You may obtain a copy of the License at
# *
# *     http://www.apache.org/licenses/LICENSE-2.0
# *
# * Unless required by applicable law or agreed to in writing, software
# * distributed under the License is distributed on an "AS IS" BASIS,
# * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# * See the License for the specific language governing permissions and
# * limitations under the License.
# */

set -e
shopt -s dotglob

DISTRIBUTEDLOG_ROOT=$(git rev-parse --show-toplevel)
GITSHA=$(git rev-parse HEAD)

# clean the dist
find . -name lib | xargs rm -r
rm -rf dist/*

# create the layout
for module in distributedlog-service distributedlog-benchmark distributedlog-tutorials/distributedlog-basic distributedlog-tutorials/distributedlog-messaging; do
    mkdir -p "dist/release/${module}/{bin,conf,lib}"
done

# build the package
mvn clean install -Ptwitter-ostrich-provider -DskipTests

# build the distributedlog-service
cp -rf distributedlog-service/bin/* dist/release/distributedlog-service/bin
cp -rf distributedlog-service/conf/* dist/release/distributedlog-service/conf
cp -rf distributedlog-service/lib/* dist/release/distributedlog-service/lib
cp -rf distributedlog-service/target/distributedlog-service-*.jar dist/release/distributedlog-service
cp -rf vagrant dist/release/

# build the distributedlog-benchmark
cp -rf distributedlog-benchmark/bin/* dist/release/distributedlog-benchmark/bin
cp -rf distributedlog-benchmark/conf/* dist/release/distributedlog-benchmark/conf
cp -rf distributedlog-benchmark/lib/* dist/release/distributedlog-benchmark/lib
cp -rf distributedlog-benchmark/target/distributedlog-benchmark-*.jar dist/release/distributedlog-benchmark

# build the distributedlog-tutorial/basic
cp -rf distributedlog-tutorials/distributedlog-basic/bin/* dist/release/distributedlog-tutorials/distributedlog-basic/bin
cp -rf distributedlog-tutorials/distributedlog-basic/conf/* dist/release/distributedlog-tutorials/distributedlog-basic/conf
cp -rf distributedlog-tutorials/distributedlog-basic/lib/* dist/release/distributedlog-tutorials/distributedlog-basic/lib
cp -rf distributedlog-tutorials/distributedlog-basic/target/distributedlog-basic-*.jar dist/release/distributedlog-tutorials/distributedlog-basic

# build the distributedlog-tutorial/messaging
cp -rf distributedlog-tutorials/distributedlog-messaging/bin/* dist/release/distributedlog-tutorials/distributedlog-messaging/bin
cp -rf distributedlog-tutorials/distributedlog-messaging/conf/* dist/release/distributedlog-tutorials/distributedlog-messaging/conf
cp -rf distributedlog-tutorials/distributedlog-messaging/lib/* dist/release/distributedlog-tutorials/distributedlog-messaging/lib
cp -rf distributedlog-tutorials/distributedlog-messaging/target/distributedlog-messaging-*.jar dist/release/distributedlog-tutorials/distributedlog-messaging

# release
cd dist/release
zip -r "distributedlog-all-${GITSHA}.zip" distributedlog-service distributedlog-benchmark distributedlog-tutorials > /dev/null
zip -r "distributedlog-service-${GITSHA}.zip" distributedlog-service >/dev/null
zip -r "distributedlog-benchmark-${GITSHA}.zip" distributedlog-benchmark >/dev/null
zip -r "distributedlog-tutorials-${GITSHA}.zip" distributedlog-tutorials >/dev/null
